﻿///<copyright>
/// Copyright © 2009 Stephen Styrchak. All Rights Reserved.
///</copyright>
using System;
using System.Reflection;
using Microsoft.Xna.Framework.Content;

namespace Ferpect.ComponentModel
{
    public class EmbeddedContentManager : ContentManager
    {
        private Assembly assembly;
        private string defaultNamespace;

        public EmbeddedContentManager(IServiceProvider serviceProvider, Assembly assembly, string defaultNamespace) : base(serviceProvider)
        {
            if (assembly == null) throw new ArgumentNullException("assembly");
            if (defaultNamespace == null) throw new ArgumentNullException("defaultNamespace");

            this.assembly = assembly;
            this.defaultNamespace = defaultNamespace;
        }

        protected override System.IO.Stream OpenStream(string assetName)
        {
            var names = this.assembly.GetManifestResourceNames();
            string name = this.defaultNamespace;

            return this.assembly.GetManifestResourceStream(name + "." + assetName + ".xnb");
        }
    }
}
